//
//  ChooseCountryViewController.m
//  HuaRun2
//
//  Created by admin on 2017/11/5.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "ChooseCountryViewController.h"
#import "ChooseCountryCell.h"
#import "ConditionModel.h"

@implementation ChooseCountryViewController

- (void)viewInit {
    [super viewInit];
    self.navigationItem.title = NSLocalizedString(@"chooseGountry",nil);

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ChooseCountryCell class]
           forCellReuseIdentifier:@"ChooseCountryCell"];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.searchView.hidden = YES;
}

- (void)layoutInit {

    [super layoutInit];
    
    weakself(ws);
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.view);
        make.bottom.mas_equalTo(ws.view).offset(0);
        make.top.mas_equalTo(ws.view).offset(kMargin10);
    }];
}

- (void)dataInit {
    
    [self fetchConfitionalByType:@"country"];
}

- (void)methodBind {

    weakself(ws);
    
    [self nbRightImageName:@"save" title:nil taget:self clickBlock:^(id result) {
        [ws submitMethod];
    }];

}

#pragma mark  UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseCountryCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseCountryCell"
                                                                      forIndexPath:indexPath];
    ConditionModel * conditionModel = self.dataArray[indexPath.row];
    cell.conditionModel = conditionModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    for (ConditionModel * conditionModel in self.dataArray) {
        conditionModel.isSelected = NO;
    }
    ConditionModel * conditionModel = self.dataArray[indexPath.row];
    conditionModel.isSelected = YES;
    [tableView reloadData];


//    ConditionModel * conditionModel = self.dataArray[indexPath.row];
//    self.clickBlock ? self.clickBlock(conditionModel): nil;
//    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark method 

- (void)searchBtnMethod {
    
}

//完成方法
- (void)submitMethod {
    
    BOOL isSelected = NO;
    ConditionModel * tConditionModel = nil;
    for (ConditionModel * conditionModel in self.dataArray) {
        if (conditionModel.isSelected) {
            tConditionModel = conditionModel;
            isSelected = YES;
            break;
        }
    }

    if (!isSelected) {
        [self showAlert:NSLocalizedString(@"chooseCountry", nil)];
        return;
    }

    self.clickBlock ? self.clickBlock(tConditionModel): nil;
    [self.navigationController popViewControllerAnimated:YES];
}



//获取筛选的条件
- (void)fetchConfitionalByType:(NSString *)type {
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kSelectConditionUrl];
    
    weakself(ws);
    NSDictionary * dic = @{@"type": type};
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            [ws showWithText:[NSString showStringWithStr:object[@"msg"]]];
            return;
        }
        NSArray * array = [NSArray yy_modelArrayWithClass:[ConditionModel class] json:object[@"obj"]];
        
        ws.dataArray = array;
        [ws.tableView reloadData];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}

//当搜索框内容为空时候，相当于执行一次刷新操作
- (void) searchFieldDidChange:(UITextField *)textField {
    if (textField.text.length == 0) {
        [self fetchConfitionalByType:@"country"];
    }
}
@end
